package com.hrt.datasender.datadao.service;

import java.util.List;

import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.orm.executer.IORMExecuter;
import com.hrt.datasender.datacollect.drive.DriveParameterMetadata;
import com.hrt.datasender.datadao.model.BcDeviceConfigModel;
import com.hrt.datasender.datadao.model.BcDevicesModel;
import com.hrt.db.DBManager;

public final class BcDeviceConfigService {

    private static volatile BcDeviceConfigService instance;

    private BcDeviceConfigService() {

    }

    public static BcDeviceConfigService getInstance() {
        if (instance == null) {
            synchronized (BcDeviceConfigService.class) {
                if (instance == null) {
                    instance = new BcDeviceConfigService();
                }
            }
        }
        return instance;
    }

    public BcDeviceConfigModel create(BcDeviceConfigModel model) {
        IORMExecuter executer = DBManager.getORMExecuter();
        executer.create(model);
        return model;
    }

    public void creates(List<BcDeviceConfigModel> models) {

        if (models == null || models.size() < 1) {
            return;
        }

        String deleteSQL = " DELETE FROM BC_DEVICE_CONFIG WHERE DEVICE_ID = @DEVICE_ID ";
        SQLParameterList pc = new SQLParameterList();
        pc.put("DEVICE_ID", models.get(0).getDeviceId());

        DBManager.getDataExecuter().delete(deleteSQL, pc);

        IORMExecuter executer = DBManager.getORMExecuter();
        executer.creates(models);
    }

    public void update(BcDeviceConfigModel model) {
        IORMExecuter executer = DBManager.getORMExecuter();
        executer.update(model);
    }

    public List<BcDeviceConfigModel> getList(String deviceId) {

        BcDevicesModel device = BcDeviceService.getInstance().getModel(deviceId);

        List<DriveParameterMetadata> driveParameterMetadatas = BcDriverService.getInstance()
                .getParameterMetadata(device.getProtocolId());

        String nameWhere = "";
        for (DriveParameterMetadata item : driveParameterMetadatas) {
            nameWhere += ",'" + item.getName() + "'";
        }
        nameWhere = "(" + nameWhere.replaceFirst(",", "") + ")";

        String sql = " SELECT * FROM BC_DEVICE_CONFIG WHERE DEVICE_ID = @deviceId and NAME IN " + nameWhere;
        SQLParameterList pc = new SQLParameterList();
        pc.put("deviceId", deviceId);

        List<BcDeviceConfigModel> result = DBManager.getORMExecuter().getModelListSQL(sql, pc,
                BcDeviceConfigModel.class);

        for (DriveParameterMetadata item : driveParameterMetadatas) {

            String name = item.getName();
            boolean bingo = false;

            for (BcDeviceConfigModel config : result) {
                if (config.getName().equals(name)) {
                    bingo = true;
                    break;
                }
            }

            if (!bingo) {
                BcDeviceConfigModel config = new BcDeviceConfigModel();
                config.setDeviceId(deviceId);
                config.setName(name);
                result.add(config);
            }
        }

        return result;

    }

}